<template>
    <div style="border: 1px solid #ccc">
        <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editorRef"
            :defaultConfig="toolbarConfig"
            :mode="mode"
        />
        <Editor
            style="height: 500px; overflow-y: hidden;"
            v-model="valueHtml"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="handleCreated"
        />
    </div>

</template>
<script setup lang="ts">
import '@wangeditor/editor/dist/css/style.css' // 引入 css

import {onBeforeUnmount, ref, shallowRef, onMounted, watch} from 'vue'
import {Editor, Toolbar} from '@wangeditor/editor-for-vue'
import {IEditorConfig,} from '@wangeditor/editor'

const editorConfig: Partial<IEditorConfig> = {  // TS 语法
// const editorConfig = {                       // JS 语法
    MENU_CONF: {},
    placeholder: '请输入...',
    // 其他属性...
}
editorConfig.MENU_CONF['uploadImage'] = {
    server: '/api/upload',
    fieldName: 'file',
    maxFileSize: 10 * 1024 * 1024,

    customInsert(res: any, insertFn: InsertFnType) {  // TS 语法
        // customInsert(res, insertFn) {                  // JS 语法
        // res 即服务端的返回结果
        console.log(res, insertFn)

        // 从 res 中找到 url alt href ，然后插入图片

        insertFn(`http://${res.data.url}`, '测试', res.data.url)
    },
    // 继续写其他配置...

    //【注意】不需要修改的不用写，wangEditor 会去 merge 当前其他配置
}

const editorRef = shallowRef()
const mode = ref('default')
// 内容 HTML
const valueHtml = ref('')
watch(() => valueHtml.value, (newVal) => {
    console.log(newVal)
})
const toolbarConfig = {}

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})

const handleCreated = (editor) => {
    editorRef.value = editor // 记录 editor 实例，重要！
}
const arr = new Array(5)
</script>

<style scoped lang="scss">

</style>
